package com.heima.api.config;


import com.heima.api.client.*;
import com.heima.api.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
//    - NONE：不记录任何日志信息，这是默认值。
//    - BASIC：仅记录请求的方法，URL以及响应状态码和执行时间
//    - HEADERS：在BASIC的基础上，额外记录了请求和响应的头信息
//    - FULL：记录所有请求和响应的明细，包括头信息、请求体、元数据。
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    /**
     * FeignClient拦截器
     * @return
     */
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if (userId != null){
                    requestTemplate.header("user-info", userId.toString());
                }

            }
        };
    }
    @Bean
    public FallbackFactory<ItemClient> itemClientFallback(){
        return new ItemClientFallback();
    }

   @Bean
    public FallbackFactory<CartClient> cartClientFallback(){
        return new CartClientFallback();
   }

   @Bean
    public FallbackFactory<TradeClient> tradeClientFallback(){
        return new TradeClientFallback();
   }

   @Bean
    public FallbackFactory<UserClient> userClientFallback(){
        return new UserClientFallback();
   }
   @Bean
    public FallbackFactory<PayClient> payClientFallback(){
        return new PayClientFallback();
   }
}
